#include "cli.h"

static const char *TAG = "CLI";
int ac_pa = 0;

void cli_main()
{
    MYAPP_LOGD(TAG, "Entering cli_main");
    get_input_domain();
    while (ac_pa == 0)
    {
        get_input_account();
        get_input_password();
    }

    get_input_title_num();

    MYAPP_LOGD(TAG, "Leaving cli_main");
}

void get_input_account()
{
    printf("Please input you account\n");
    input_cycle(0, 0);
}

void get_input_title_num()
{
    print_title();
    input_cycle(1, 0);
}

void get_input_title_num_low()
{
    print_return();
    input_cycle(2, 0);
}

void get_input_password()
{
    printf("Please input your password\n");
    input_cycle(0, 1);
}

void get_input_domain()
{
    printf("Please input the pop3 domain\n");
    input_cycle(0, 2);
}

// mode: 0 for string / 1 for num 1-7 / 2 for num 1-2
// key : 0 for account / 1 for password (only in mode string) / 2 for domain
void input_cycle(int mode, int key)
{
    MYAPP_LOGD(TAG, "Inside input cycle");
    int *return_flag_ptr = &return_flag;
    char buf[BUFFSIZE];
    memset(buf, 0x00, BUFFSIZE);

    while (!return_flag)
    {
        printf("[popshell]$ ");
        // get input from keyboard
        int input_num;
        if (key == 1)
        {
            input_num = get_input_hide(buf);
        }
        else
        {
            input_num = get_input(buf);
        }

        if (input_num == 0)
            continue;
        if (mode == 1)
            if (input_num != 1)
            {
                printf("Please input only one digit\n");
                continue;
            }
            else
            {
                parse_int(buf, return_flag_ptr, 0);
            }
        else if (!mode)
            parse_string(buf, return_flag_ptr, key);
        else
        {
            if (input_num != 1)
            {
                printf("Please input only one digit\n");
                continue;
            }
            else
            {
                parse_int(buf, return_flag_ptr, 1);
            }
        }
    }
    return_flag = 0;
    MYAPP_LOGD(TAG, "Outside input cycle");
}

int input_email_num()
{
    printf("[popshell]$ ");
    char buf[BUFFSIZE];
    memset(buf, 0x00, BUFFSIZE);
    get_input(buf);
    return buf[0] - '0';
}

char *input_email_name()
{
    printf("[popshell]$ ");
    char *buf = (char *)malloc(40);
    memset(buf, '\0', 40);
    get_input(buf);
    return buf;
}

int get_input(char buf[])
{
    // get input from keyboard and assigning to buf
    fgets(buf, BUFFSIZE, stdin);
    // reform \n to \0
    buf[strlen(buf) - 1] = '\0';

    return strlen(buf);
}

int get_input_hide(char buf[])
{
    // get input from keyboard and assigning to buf
    // fgets(buf, BUFFSIZE, stdin);
    int i = 0;
    char ch;
    system("stty -icanon -echo");
    while (1)
	{
		ch = getchar();
		buf[i++] = ch;
		if (ch=='\n')//getch()函数如果读到回车符号返回'/r'
		{
			buf[--i] = '\0';
			break;
		}
		printf("*");
	}
    printf("\n");

    system("stty icanon echo");
    return strlen(buf);
}

// mode: 0 for 1-7 / 1 for 1-2
void parse_int(char *buf, int *return_flag, int mode)
{
    int in_check = checkinput(buf, 1);
    if (in_check == 1)
    {
        int num = buf[0] - '0';
        if (!mode)
        {
            switch (num)
            {
            case 1:
                get_list(1);
                get_input_title_num_low();
                print_title();
                break;
            case 2:
                get_status();
                get_input_title_num_low();
                print_title();
                break;
            case 3:
                // display_subjects();
                printf("%s\n", display_subjects());
                get_input_title_num_low();
                print_title();
                break;
            case 4:
                search_text();
                get_input_title_num_low();
                print_title();
                break;
            case 5:
                displaybysubjects();
                get_input_title_num_low();
                print_title();
                break;
            case 6:

                download_mail();
                get_input_title_num_low();
                print_title();
                break;
            case 7:
                exit(1);

            default:
                printf("Wrong num input! Please input a num between 1-7 \n");
                MYAPP_LOGE(TAG, "Wrong num input!");
                break;
            }
        }
        else
        {
            switch (num)
            {
            case 1:
                *return_flag = 1;
                break;
            case 2:
                exit(1);
            default:
                printf("Wrong num input! Please input a num between 1-2 \n");
                MYAPP_LOGE(TAG, "Wrong num input!");
                break;
            }
        }
    }
    else
    {
        printf("Wrong input, please input a digit\n");
    }
}

void parse_string(char *buf, int *return_flag, int key)
{
    switch (key)
    {
    case 0:
    {
        int ret = verify_account(buf);
        if (ret)
            *return_flag = 1;
        break;
    }
    case 1:
    {
        int ret = verify_password(buf);
        if (ret == 1)
        {
            ac_pa = 1;
        }
        else
        {
            ac_pa = 0;
        }
        if (ret)
            *return_flag = 1;
        break;
    }
    case 2:
    {
        int ret = verify_domain(buf);
        if (ret)
            *return_flag = 1;
        break;
    }

    default:
        MYAPP_LOGE(TAG, "Wrong num input!");
        break;
    }
}

void print_title()
{
    system("clear");
    printf("*******************************************\n");
    printf("1.Get a list of messages and sizes\n");
    printf("2.Get mail status\n");
    printf("3.Display mail status\n");
    printf("4.Search text in all emails\n");
    printf("5.Display by subjects\n");
    printf("6.Download the mail and delete in the remote serach\n");
    printf("7.quit\n");
    printf("*******************************************\n");
}

void print_return()
{
    printf("Press 1 to return to main interface\n");
    printf("Press 2 to return to quit\n");
}
